package v1

import (
	"fmt"
	"gin-project/model"
	"gin-project/utils/errmsg"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

func InsertSong(ctx *gin.Context) {
	singerIDStr := ctx.PostForm("singerId")
	singerID, _ := strconv.Atoi(singerIDStr)
	name := ctx.PostForm("name")
	lyric := ctx.PostForm("lyric")
	introduction := ctx.PostForm("introduction")
	file, header, _ := ctx.Request.FormFile("file")
	url, _ := model.UploadSongUrl(file, header)

	var song model.Song
	song.SingerID = singerID
	song.Name = name
	song.Lyric = lyric
	song.Introduction = introduction
	song.Url = url

	code := model.InsertSong(&song)
	ctx.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": errmsg.GetErrMsg(code),
	})
	return
}

func SelectSongsBySingerID(ctx *gin.Context) {
	singerID := ctx.Query("singerId")
	singer, code := model.SelectSongsBySingerID(singerID)
	ctx.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": errmsg.GetErrMsg(code),
		"data": singer,
	})
	return
}

func UpdateSong(ctx *gin.Context) {
	id := ctx.PostForm("id")
	name := ctx.PostForm("name")
	introduction := ctx.PostForm("introduction")
	lyric := ctx.PostForm("lyric")

	code := model.UpdateSong(map[string]interface{}{
		"name": name,
		"introduction": introduction,
		"lyric": lyric,
	}, id)

	ctx.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": errmsg.GetErrMsg(code),
	})
	return
}

func DeleteSong(ctx *gin.Context) {
	id := ctx.Query("id")
	code := model.DeleteSong(id)
	ctx.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": errmsg.GetErrMsg(code),
	})
	return
}

func UpdateSongPic(ctx *gin.Context) {
	file, header, err := ctx.Request.FormFile("file")
	id := ctx.PostForm("id")

	if err != nil {
		fmt.Println("获取图片失败。。。")
		ctx.JSON(http.StatusOK, gin.H{
			"code": errmsg.ERROR,
			"msg" : errmsg.GetErrMsg(errmsg.ERROR),
		})
		return
	}

	url, _ := model.UpdateSongPic(file, header)
	code := model.UpdateSong(map[string]interface{}{
		"pic": url,
	}, id)

	ctx.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": errmsg.GetErrMsg(code),
	})
	return
}

func UpdateSongUrl(ctx *gin.Context) {
	file, header, _ := ctx.Request.FormFile("file")
	id := ctx.PostForm("id")

	var code int = 200

	go func() {
		url, _ := model.UpdateSongUrl(file, header)
		code = model.UpdateSong(map[string]interface{}{
			"url": url,
		}, id)
	}()

	ctx.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": errmsg.GetErrMsg(code),
	})
	return
}

func SelectSongByName(ctx *gin.Context) {
	name := ctx.Query("songName")

	song, code := model.SelectSongByName(name)
	ctx.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": errmsg.GetErrMsg(code),
		"data": song,
	})
	return
}

func SelectSongAll(ctx *gin.Context) {
	songs, code := model.SelectSongAll()
	ctx.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": errmsg.GetErrMsg(code),
		"data": songs,
	})
	return
}

func SelectLikeName(ctx *gin.Context) {
	name := ctx.Query("songName")
	songs, code := model.SelectLikeName(name)
	ctx.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": errmsg.GetErrMsg(code),
		"data": songs,
	})
	return
}

func SelectSongByID(ctx *gin.Context) {
	songID := ctx.Query("songId")
	fmt.Println(songID)

	song, code := model.SelectSongByID(songID)
	ctx.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": errmsg.GetErrMsg(code),
		"data": song,
	})
}